<template>
	<w-container :diyNav="true">
		<view class="flex flex-js-sb flex-ai-center flex-1 plr-16" slot='nav'>
			<view class="font-size-14 font-color-textPrimary" @click="back">
				{{handleLang('取消')}}
			</view>
			<w-button size="ss" style='min-width: 104rpx;' :disabled="disbaled" @click='submit'>{{handleLang('发表')}}</w-button>
		</view>
		<view class="plr-16">
			<view class="bg-color-item">
				<u--textarea v-model="form.content" color="#222" customStyle="{color:'#222'}" border='none' confirmType='done' :maxlength='-1'  :placeholder="handleLang('请输入内容')" ></u--textarea>
			</view>
			<view class="">  
					<u-upload
						:fileList="form.images"
						@delete="deleteImage"
						multiple
						@afterRead='uploadImage'
						:maxCount="3"
						width='200rpx'
						height='200rpx'
					>
						<view class="flex flex-center flex-dir-col bg-color-item mt-16" style="width: 200rpx;height: 200rpx;">
							<w-icon name="img" size="32" v-show="form.images.length==0"></w-icon>
							<text class="font-size-10" style="color:#bababa" v-if="form.images.length==0">{{handleLang('照片')}}</text>
							<w-icon name="plus" size="32" color='#8b8b8b' v-else></w-icon>
						</view>
					</u-upload>
			</view>
			<view class="flex flex-ai-center pt-32">
				<text class="font-color-minor font-size-14 mr-12">{{handleLang('匿名发表')}}</text>
				<u-switch size='16' inactiveColor="rgb(230, 230, 230)" active-color="#FDF251" :activeValue='1' :inactiveValue='0' v-model="form.is_anonymous" ></u-switch>
			</view>
		</view>
	</w-container>
</template>

<script>
	import {uploadFile} from '@/utils/upload.js'
	import {sendRumor} from '@/api/notice/index.js'
	export default {
		data() {
			return {
				form:{
					content:'',
					images:[],
					is_anonymous:0,
				}
			};
		},
		computed:{
			disbaled(){
				return !this.form.content
			}
		},
		methods:{
			//删除图片
			deleteImage({index}){
				this.form.images.splice(index,1);
			},
			//读取图片
			uploadImage({file}){
				this.form.images=this.form.images.concat(file)
			},
			back(){
				uni.navigateBack()
			},
			async uploadImages(){
				if(this.form.images.length<1) return;
				uni.showLoading({
					title:"正在上传图片"
				})
				let images=[]
				for(let i=0;i<this.form.images.length;i++){
					let {name,url}=this.form.images[i];
					let type=this.$tools.getFileNameExt(name)
					let res= await uploadFile(url,type);
					images.push(res);
				}
				return images.join(',')
			},
			async submit(){
				
				let imgsStr=await this.uploadImages();
				uni.showLoading({
					title:"发表中"
				})
				let data=this.$tools.copyObj(this.form);
				data.images=imgsStr;
				sendRumor(data).then(res=>{
					uni.showToast({
						title:res.msg,
						success(){
							setTimeout(()=>{
								uni.navigateBack()
							},1500)
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .u-textarea {
		@extend .bg-color-item;
	}
	/deep/.uni-textarea-textarea{
		color: #222;
	}
</style>
